//
//  XHDynamicNewsViewController.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-7-29.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "XHDynamicNewsViewController.h"
#import "XHDynamicViewModel.h"
#import "XHTableViewHeaderView.h"
#import <MBProgressHUD.h>
#import "MainEnterViewController.h"

@interface tabVliewCell1 : UITableViewCell


@end


@interface XHDynamicNewsViewController ()
@property (nonatomic ,strong) IBOutlet UITableView *tableView;
@property (nonatomic ,strong) XHDynamicViewModel *viewModel;
@property (nonatomic ,strong) MBProgressHUD *HUD;

@end

#define LIST @[@"中国网事",@"山西新闻",@"地市热点",@"县区快报",@"厅级资讯"]

@implementation XHDynamicNewsViewController


- (instancetype)init
{
    self = [super init];
    if (self) {
        NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"DynamicNewsView" owner:self options:nil];
        UIView *view = [array objectAtIndex:0];
        CGRect rect = self.view.frame;
        CGFloat oY = kIOS_VERSION<7.0?44:64;
        self.titleLable.text = @"新闻动态";
        [view setFrame:CGRectMake(0, oY, rect.size.width, rect.size.height-oY)];
        [self.view addSubview:view];
        @weakify(self);
        [[self.leftButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [self.leftButton setImage:[UIImage imageNamed:@"icon_back@2x"] forState:UIControlStateNormal];
        
        self.rightButton.hidden = YES;
        
        self.viewModel = [[XHDynamicViewModel alloc] init];
        NSUserDefaults* defa = [NSUserDefaults standardUserDefaults];
        
        self.viewModel.titleArray = [defa objectForKey:@"新闻动态栏目"];
        self.viewModel.tempdataArray = [defa objectForKey:@"新闻动态新闻"];
        self.tableView.dataSource = self.viewModel;
        self.tableView.delegate = self;
        self.tableView.frame = CGRectMake(10, oY+5, 300, self.view.frame.size.height-oY-5);
        self.tableView.bounces = NO;
        [RACObserve(self.viewModel, hasLoad) subscribeNext:^(id x) {
            if ([x intValue]) {
                @strongify(self);
                [self performSelectorOnMainThread:@selector(refresh) withObject:nil waitUntilDone:YES];
            }
        }];
        
        self.HUD = [[MBProgressHUD alloc] initWithView:view];
        [view addSubview:self.HUD];
       
        self.HUD.labelText = @"正在加载";
        self.HUD.dimBackground = YES;
        [self.HUD show:YES];
        if ([self.viewModel.titleArray count] > 0)
        {
            [self refresh];
        }
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)refresh
{
    [self.tableView reloadData];
    [self.HUD hide:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.0;
}

/*- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"XHTableViewHeader" owner:self options:nil];
    XHTableViewHeaderView *view = [array objectAtIndex:0];
    view.titleLable.text = [LIST objectAtIndex:section];
    return view;
}*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0)
    {
        wangshiViewController* v = [[wangshiViewController alloc] init];
        [self.navigationController pushViewController:v animated:YES];
    }
    else
    {
       
        MainEnterViewController* viewControoler = [[MainEnterViewController alloc] init];
        viewControoler.damokuai = @"新闻动态";
        viewControoler.yinitData = [self.viewModel.titleArray objectAtIndex:indexPath.row];
        NSLog(@"yinit = %@",viewControoler.yinitData);
        [self.navigationController pushViewController:viewControoler animated:YES];
        butoncl* bl = [[butoncl alloc] init];
        NSString* str = [viewControoler.yinitData[@"id"] stringValue];
        [bl postString:@"001" fff:str];
    }
    
}
@end
